<template>
  <el-container>
    <el-header style="text-align: left; font-size: 12px">
      <span>全部动态</span>>
    </el-header>
    <el-main>
      <el-table :data="tableData" >
        <el-table-column prop="createTime" label="时间" width="140">
        </el-table-column>
        <el-table-column prop="likes" label="点赞数量" width="120">
        </el-table-column>
        <el-table-column prop="comments" label="评论数" width="120">
        </el-table-column>
        <el-table-column prop="body" label="动态内容">
        </el-table-column>
        <el-table-column prop="user.name" label="用户">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="danger"
                @click="handleEdit2(scope.$index, scope.row)">删除动态
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-dialog width="40%" title="删除动态" :visible.sync="dialogFormVisible2">
      确实删除动态吗
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="deleteDynamic">确 定</el-button>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
import axios from "axios";
import {mapState} from "vuex";

export default {
  name: "dynamicAdmin",
  data() {
    return {
      tableData: [],
      dialogFormVisible2: false,
      form: {},
      kind: [],
      formLabelWidth: '100px',
      selected: '',
      id: '',
      message: '',
      sum: '',
      requests: []
    }
  },
  methods: {
    //删除动态
    handleEdit2(index, row) {
      this.dialogFormVisible2 = true
      this.id = row.id
      console.log(this.id)
    },
    //获取全部用户的方法
    getAllDynamic() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      axios.get(`/api/admin/dynamic`).then(
          response => {
            if (response.data.code == 200) {
              loading.close();
              this.tableData = response.data.data
              console.log(this.tableData)
            } else {
              loading.close();
              this.$message.error(response.data.errorMsg);
            }
          },
          error => {
            //请求失败
            loading.close();
            this.$message.error('服务繁忙');
          }
      )
    },
    deleteDynamic() {
      axios.delete(`/api/admin/dynamic/`+this.id,).then(
          response => {
            if (response.data.code == 200) {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.dialogFormVisible2 = false
              this.getAllDynamic()
            } else {
              this.$message.error(response.data.errorMsg);
            }
          },
          error => {
            //请求失败
            this.$message.error('服务繁忙');
          }
      )
    },
  },
  mounted() {
    this.getAllDynamic()

  }
}
</script>

<style scoped>
.el-header {
  background-color: lightsteelblue;
  color: #333;
  line-height: 60px;
}
</style>
